/*
 *
 * 目录
 */

import { createAPI } from '@/utils/request'

// 目录列表
// export const list = data => createAPI('/directorys', 'get', data)
export const getDirectoryList = function(data) {
  const stateMapping = new Map([
    [1, '已启用'],
    [0, '已禁用'],
  ])
  return createAPI('/directorys', 'get', data)
  .then(res => ({
    counts: res.data.counts,
    items: res.data.items.map((d, i) => ({
      ...d,
      addDate: d.addDate.substring(0, 19).replace('T', ' '),
      index: (data.page - 1) * data.pagesize + i + 1,
      state: stateMapping.get(d.state),
    })),
  }))
  .catch(err => err)
}

// 目录简单列表
export const getDirectorysSimple = data => createAPI('/subjects/simple', 'get', data)

// 目录详情
export const getDirectorysDetail = data => createAPI(`/directorys/${data.id}`, 'get', data)

// 目录添加
export const addDirectory = data => createAPI('/directorys', 'post', data).then(res => res).catch(err => Promise.reject(err.response.message))

// 目录修改
export const updateDirectory = data => createAPI(`/directorys/${data.id}`, 'put', data)

// 目录删除
export const removeDirectory = data =>
  createAPI(`/directorys/${data.id}`, 'delete', data)

// 目录状态
export const changeState = data => createAPI(`/directorys/${data.id}/${data.state}`, 'post', data)

// 根据id获取目录列表
export const getDirectorysSimpleList = data => createAPI('/directorys/simple', 'get', data)
